package com.ace_server.controller;

import com.ace_server.model.Group;
import com.ace_server.service.GroupService;
import com.solution.framework.controller.BaseController;
import com.solution.framework.util.*;
import org.hibernate.criterion.Criterion;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with generator tools.
 * User: sunguangzhu
 * DateTime:2014-02-19 05:00:53
 */
@Controller
public class GroupController extends BaseController<Group> {

    private static final String path_index = "group/index";
    private static final String path_grid = "group/grid";
    private static final String path_input = "group/input";
    private static final String path_view = "group/view";
    private static final String path_tree = "group/tree";

    @Resource
    private GroupService groupService;

    @RequestMapping
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("index method ....");
        return path_index;
    }

    @RequestMapping
    public String grid(Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("grid method ....");
        Pagination pagination = RequestVarsUtil.getPagination(request);
        List<Criterion> criterions = this.getSearchCriterions(request);
        List<Group> entitys = this.groupService.listByCriteria(pagination, criterions);
        int total = this.groupService.getTotalByCriteria(criterions);
        RequestVarsUtil.setPagination(pagination, total);
        request.setAttribute("pageModel", pagination);
        model.addAttribute("list", entitys);
        return path_grid;
    }

    @RequestMapping
    public void tree(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("tree method ....");
        ArrayList<ZTreeModel> zTreeModels = new ArrayList<ZTreeModel>();
        ZTreeModel root = ZTreeModel.getRoot(Constant.group_tree);
        List<Group> groups = this.groupService.listByHql();
        for (Group group : groups) {
            ZTreeModel zTreeModel = new ZTreeModel();
            zTreeModel.setId(group.getId());
            if (group.getParent() == null) {
                zTreeModel.setPid(root.getId());
            } else {
                zTreeModel.setPid(group.getParent().getId());
            }
            zTreeModel.setName(group.getName());
            zTreeModels.add(zTreeModel);
        }
        String results = JsonUtil.transformObjectToString(zTreeModels);
        MessageDto messageDto = new MessageDto();
        super.setMessageDto(messageDto, results);
        super.writeToClient(response, messageDto.toString());
    }

    @RequestMapping
    public String add(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("add method ....");
        Group group = new Group();
        model.addAttribute("bean", group);
        return path_input;
    }

    @RequestMapping
    public String edit(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("edit method ....");
        Group group = this.groupService.get(id);
        model.addAttribute("bean", group);
        return path_input;
    }

    @RequestMapping
    public String view(Long id, Model model, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("view method ....");
        Group group = this.groupService.get(id);
        model.addAttribute("bean", group);
        return path_view;
    }

    @RequestMapping
    @DateTimeFormat
    public void save(Group group, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("save method ....");
        MessageDto messageDto = new MessageDto();
        try {
            if (group.getId() != null) {
                Group target = this.groupService.get(group.getId());
                BeanUtils.copyProperties(group, target, new String[]{
                        "createUser",
                        "createTime",
                });
                this.groupService.updateceshi(target);
//                this.groupService.update(target);
//                Group group1 = this.groupService.get(target.getId());
//                this.groupService.save(group1);
                super.setMessageDto(messageDto, MessageDefined.update_success_info);
            } else {
                this.groupService.save(group);
                super.setMessageDto(messageDto, MessageDefined.save_success_info);
            }
        } catch (DataAccessException e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        try {
            super.writeToClient(response, messageDto.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping
    protected void delete(Long id, Model model, HttpServletRequest request, HttpServletResponse response) throws
            IOException {
        MessageDto messageDto = new MessageDto();
        try {
            this.groupService.delete(id);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
        }
        super.writeToClient(response, messageDto.toString());
    }

    @ResponseBody
    @RequestMapping
    protected void deleteMore(String ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        MessageDto messageDto = new MessageDto();
        try {
            Integer count = this.groupService.delete(ids);
            super.setMessageDto(messageDto, MessageDefined.del_success_info);
        } catch (Exception e) {
            messageDto.setSuccess(false);
            super.setMessageDto(messageDto, MessageDefined.sys_error_info);
            e.printStackTrace();
        }
        super.writeToClient(response, messageDto.toString());
    }
}
